package com.ailesson.video.service.impl;

import com.ailesson.common.except.GlobalErrorCodeConstants;
import com.ailesson.common.except.ServiceException;
import com.ailesson.video.model.dto.VideoVisitorCountDto;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import com.ailesson.video.mapper.VideoVisitorMapper;
import com.ailesson.video.entity.VideoVisitorEntity;
import com.ailesson.video.service.VideoVisitorService;
import com.ailesson.video.model.req.VideoVisitorQueryReq;
import com.ailesson.video.model.req.VideoVisitorReq;
import com.ailesson.video.model.reply.VideoVisitorReply;
import com.ailesson.common.reply.PageResult;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
public class VideoVisitorServiceImpl extends ServiceImpl<VideoVisitorMapper, VideoVisitorEntity> implements VideoVisitorService {

    @Override
    public PageResult queryPage(VideoVisitorQueryReq params) {
       Page pageTmp = Page.of(params.getPageNo(),params.getPageSize());
       IPage<VideoVisitorEntity> page = this.page(
               pageTmp,
               new LambdaQueryWrapper<VideoVisitorEntity>().orderByDesc(VideoVisitorEntity::getId)
       );
       List<VideoVisitorReply> replyList = Collections.EMPTY_LIST;
       if (CollectionUtil.isNotEmpty(page.getRecords())){
           replyList = page.getRecords().stream().map(x -> {
               VideoVisitorReply reply = new VideoVisitorReply();
               BeanUtils.copyProperties(x, reply);
               return reply;
           }).collect(Collectors.toList());
       }
       return new PageResult<>(replyList,page.getTotal());
    }

    @Override
    public VideoVisitorReply info(Long id) {
        VideoVisitorEntity entity = this.getById(id);
        if (entity != null){
            VideoVisitorReply reply = new VideoVisitorReply();
            BeanUtils.copyProperties(entity, reply);
            return reply;
        }
        return null;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void update(VideoVisitorReq videoVisitor) {
        if (videoVisitor.getId() == null || videoVisitor.getId() <= 0){
            throw new ServiceException(GlobalErrorCodeConstants.ID_EMPTY_ERROR);
        }
        VideoVisitorEntity entity = this.getById(videoVisitor.getId());
        if (entity == null){
            throw new ServiceException(GlobalErrorCodeConstants.NOT_EXISTS_ERROR);
        }
        BeanUtils.copyProperties(videoVisitor, entity);
        this.updateById(entity);
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public Long add(VideoVisitorReq videoVisitor) {
        videoVisitor.setId(null);
        VideoVisitorEntity entity = new VideoVisitorEntity();
        BeanUtils.copyProperties(videoVisitor, entity);
        this.save(entity);
        return entity.getId();
    }

    @Override
    public Map<String, Object> queryCount(Long courseId, String startDate, String endDate) {
        List<VideoVisitorCountDto> list = this.baseMapper.queryCount(courseId, startDate, endDate);
        //创建map集合
        Map<String, Object> map = new HashMap<>();
        //创建两个list集合，一个代表所有日期，一个代表日期对应数量
        //封装数据  代表所有日期
        List<String> dateList =
                list.stream().map(VideoVisitorCountDto::getJoinTime).
                        collect(Collectors.toList());
        //代表日期对应数量
        List<Integer> countList = list.stream().map(VideoVisitorCountDto::getUserCount)
                .collect(Collectors.toList());
        //放到map集合
        map.put("xData", dateList);
        map.put("yData", countList);
        return map;
    }
}